const app = getApp()
const api = require('../../utils/api.js')
const util = require('../../utils/util.js')

Page({
    data:{
        rid:'',
        tid:'',
        dishes:[],
        total:0,
        count:0,
        page:1,
        orderList:[],
        openId:''
    },
    onShow:function(){
        this.refresh()
    },
    onLoad:function(option){
        console.log(option)
        this.setData({
            rid:option.rid,
            tid:option.tid
        })
    },
    countChange:function(e){
        var that = this
        const dish = e.currentTarget.dataset.item
        var currentPrice = e.currentTarget.dataset.item.price

        if(e.detail.tag === 'plus'){
            this.setData({
                total:that.data.total+currentPrice,
                count:that.data.count+1,
                orderList:that.data.orderList.concat(dish)
            })
        }else{
            this.setData({
                total:that.data.total-currentPrice,
                count:that.data.count-1,
                orderList:util.arrRemove(that.data.orderList,dish)
            })
        }

    },
    onPullDownRefresh:function(){
        this.refresh()
    },
    refresh:function(){
        this.setData({
            page:1
        })
        var that = this
        wx.showLoading({
            title:"拼命加载中"
        })
        api.GET({
            url:api.url.dishes,
            params:{
                rid:that.data.rid,
                page:1,
                size:12
            },
            success:function(data){
                console.log(data)
                that.setData({
                    dishes:data
                })
                wx.stopPullDownRefresh()
                wx.hideLoading()
            },
            fail:function(err){
                wx.stopPullDownRefresh()
                wx.hideLoading()
            }
        })
    },
    getMore:function(page){
        if(this.data.dishes.length%12 != 0){
            return;
        }
        var that = this
        wx.showLoading({
            title:"拼命加载中"
        })
        api.GET({
            url:api.url.dishes,
            params:{
                rid:that.data.rid,
                page:page,
                size:12
            },
            success:function(data){
                that.setData({
                    dishes:that.data.dishes.concat(data)
                })
                wx.stopPullDownRefresh()
                wx.hideLoading()
            },
            fail:function(err){
                wx.stopPullDownRefresh()
                wx.hideLoading()
            }
        })
    },
    onReachBottom:function(){
        this.setData({
            page:this.data.page+1
        })
        this.getMore(this.data.page)
    },
    settle:function(){
        console.log(this.data.orderList)
        if(this.data.orderList.length === 0){
            wx.showToast({
                title:"您还没有点单",
                icon:"none"
            })
            return
        }
        var alert = []
        var dids = []
        for(var i=0;i!=this.data.orderList.length;++i){
            alert.push(this.data.orderList[i].name)
            dids.push(this.data.orderList[i].id)
        }
        var that = this
        console.log(alert)
        wx.getStorage({
            key: 'openId',
            success: function(res){
                that.setData({
                    openId:res.data
                })
            }
        })
        wx.showModal({
            title:"订单信息",
            content:"桌号："+that.data.tid+"\n"+alert.join("\n"),
            success:function(res){
                if(res.confirm){
                    console.log("确定")
                    console.log(that.data.tid)
                    api.POST({
                        url:api.url.createOrder,
                        params:{
                            rid:that.data.rid,
                            tid:that.data.tid,
                            dids:dids,
                            total:that.data.total,
                            openId:that.data.openId
                        },
                        success:function(data){
                            console.log(data)
                        },
                        fail:function(err){

                        }
                    })
                }else{
                    console.log("取消")
                }
            }
        })
    }
})